<template>
  <div>
    <el-row v-if="optType != 'view'" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="workstationworkerList" @selection-change="handleSelectionChange">
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="岗位" align="center" prop="postName" />
      <el-table-column label="电话" align="center" prop="phonenumber" />
      <el-table-column label="操作" align="center" v-if="optType !='view'" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:workstation:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workstation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[2]"
      @pagination="getList"
    />
    <WorkstationworkerSelect ref="workerSelect" @onSelected="onWorkerAdd"></WorkstationworkerSelect>
  </div>
</template>

<script>
import {addWorkstationworker, delWorkstationworker, listWorkstationworker} from "@/api/mes/md/workstationworker";
import WorkstationworkerSelect from "@/components/userSelect/multi.vue";

export default {
  name: "Workstationworker",
  components: {WorkstationworkerSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人力资源表格数据
      workstationworkerList: [],
      postOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        workstationId: this.workstationId,
      },
      // 表单参数
      form: {},
    };
  },
  props: {
    workstationId: undefined,
    optType: undefined,
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人力资源列表 */
    getList() {
      this.loading = true;
      listWorkstationworker(this.queryParams).then(response => {
        this.workstationworkerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除此资源').then(function() {
        return delWorkstationworker(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.workerSelect.showFlag = true;
    },
    onWorkerAdd(rows) {
      this.loading = true;
      let data = [];
      rows.forEach(item => {
        data.push({
          workstationId: this.workstationId,
          userId: item.userId
        })
      })
      addWorkstationworker(data).then(response => {
        this.getList();
        this.loading = false;
      }).catch(
        () => {
          this.loading = false;
        }
      )
    },
  }
};
</script>
